﻿Option Strict On
Option Explicit On

Imports System.Net.Mail
Imports System.Drawing

Public Class RecoverPassword
    Inherits System.Web.UI.Page
    Private ws As New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice
    Private ds As DataSet
    Private userid As String
    Private pw As String
    Private Q1 As String
    Private A1 As String
    Private Q2 As String
    Private A2 As String
    Private fname As String
    Private lname As String
    Private email As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            btnSubmit.Text = "Submit"
            btnSubmit.Attributes.Add("onclick", "return validateEmail();")
            lblEnterEmail.Visible = True
            lblSecurityQ1.Visible = False
            lblSecurityQ2.Visible = False
            txtEmail.Visible = True
            txtSecurityA1.Visible = False
            txtSecurityA2.Visible = False
            btnSubmit.Visible = True
            lblMsg.Visible = False
            lblInstruction.Visible = False
        End If
    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        email = txtEmail.Text.Trim
        If btnSubmit.Text = "Submit" Then

            Try
                ds = ws.findUser(email)
                If ds.Tables(0).Rows.Count > 0 Then
                    userid = CStr(ds.Tables(0).Rows(0)("UserID"))
                    Dim ht As Hashtable = UserInfoHT(ds, userid)
                    Session.Add("InfoHT", ht)
                Else
                    lblMsg.Text = "The email entered was not found in database. You may have used a different email to register!"
                    lblMsg.Visible = True
                    txtEmail.Text = ""
                    txtEmail.Focus()
                    Exit Sub
                End If
            Catch ex As Exception
                lblMsg.Text = "Connection error. Try again"
                lblMsg.Visible = True
                Exit Sub
            End Try

            btnSubmit.Text = "Email my credentials"
            lblSecurityQ1.Text = Q1
            lblSecurityQ2.Text = Q2
            lblSecurityQ1.Visible = True
            lblSecurityQ2.Visible = True
            txtSecurityA1.Visible = True
            txtSecurityA2.Visible = True
            lblEnterEmail.Visible = False
            txtEmail.Visible = False
            lblMsg.Visible = False
            txtSecurityA1.Focus()

        ElseIf btnSubmit.Text = "Email my credentials" Then

            Dim htable As Hashtable = CType(Session("InfoHT"), Hashtable)

            If Not txtSecurityA1.Text.Trim = CStr(htable("A1")) Then
                lblMsg.Text = "Incorrect answer to the first security question!"
                lblMsg.Visible = True
                txtSecurityA1.Focus()
                Exit Sub
            ElseIf Not txtSecurityA2.Text.Trim = CStr(htable("A2")) Then
                lblMsg.Text = "Incorrect answer to the second security question!"
                lblMsg.Visible = True
                txtSecurityA1.Focus()
                Exit Sub
            ElseIf txtSecurityA1.Text.Trim = CStr(htable("A1")) AndAlso txtSecurityA2.Text.Trim = CStr(htable("A2")) Then

                lblMsg.Text = "Thank you " & CStr(htable("fname")) & " " & CStr(htable("lname")) & ", an email with your credentials has been sent to you."
                lblMsg.ForeColor = Color.Blue
                lblMsg.Visible = True
                lblSecurityQ1.Visible = False
                lblSecurityQ2.Visible = False
                txtSecurityA1.Visible = False
                txtSecurityA2.Visible = False
                btnSubmit.Visible = False
                Try
                    Dim objSmtp As New SmtpClient("smtp.temple.edu")
                    Dim mailTo As New MailAddress(email)
                    Dim mailFrom As New MailAddress("CredentialRecovery@BeyondDARS.com")
                    Dim objMail As New MailMessage(mailFrom, mailTo)
                    objMail.Subject = "Your Beyond DARS Credentials"
                    objMail.IsBodyHtml = True
                    Dim body As String = ""
                    body += "Hi " & CStr(htable("fname")) + ", <BR/>" + "Please find your Beyond DARS username and password as you requested :" + "<BR></BR>"
                    body += "Password: " + CStr(htable("pw")) + "<BR/>"
                    body += "User name : " + CStr(htable("userid")) + "<BR/>" + "Thank you,"
                    objMail.Body = body
                    objSmtp.Send(objMail)
                Catch ex As Exception
                    lblMsg.Text = ex.Message
                End Try

            End If

        End If

    End Sub
    Private Function UserInfoHT(ByVal dset As DataSet, ByVal userId As String) As Hashtable
        Dim ht As New Hashtable

        fname = CStr(dset.Tables(0).Rows(0)("FirstName"))
        lname = CStr(dset.Tables(0).Rows(0)("LastName"))

        ds = ws.getSecurityQA(userId)

        Q1 = CStr(ds.Tables(0).Rows(0)("SecurityQ1"))
        A1 = CStr(ds.Tables(0).Rows(0)("SecurityA1"))
        Q2 = CStr(ds.Tables(0).Rows(0)("SecurityQ2"))
        A2 = CStr(ds.Tables(0).Rows(0)("SecurityA2"))
        pw = CStr(ds.Tables(0).Rows(0)("Password"))

        ht.Add("userId", userId)
        ht.Add("fname", fname)
        ht.Add("lname", lname)
        ht.Add("Q1", Q1)
        ht.Add("A1", A1)
        ht.Add("Q2", Q2)
        ht.Add("A2", A2)
        ht.Add("pw", pw)

        Return ht
    End Function

End Class